using MyApiApp.Api.Db;
using MyApiApp.Api.Entity;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
using System.Linq;
using System;
namespace MyApiApp.Api.Repository
{
    public abstract class EfRepository<T> : IRepository<T> where T : BaseEntity
    {
        private MyApiApp.Api.Db.EntityDb db = new Db.EntityDb();
        private DbSet<T> _entity;

        protected DbSet<T> Entity
        {
            get
            {
                if (_entity == null)
                {
                    _entity = db.Set<T>();
                }
                return _entity;
            }
        }

        IQueryable<T> IRepository<T>.Table
        {
            get
            {
                return Entity.AsQueryable<T>();
            }
        }

        public void Delete(int id)
        {
            var t = Entity.Where(x => x.Id == id).FirstOrDefault();
            Entity.Remove(t);
            db.SaveChanges();
        }

        public T GetById(int id)
        {
            var t = Entity.Where(x => x.Id == id).FirstOrDefault();
            return t;
        }


        public void Insert(T entity)
        {
            if (entity == null)
            {
                throw new System.NullReferenceException();
            }

        }


        public Task InsertAsync(T entity)
        {
            throw new System.NullReferenceException();
        }

        public void Updata(T entity)
        {
            throw new System.NullReferenceException();
        }

        public Task UpdataAsync(T entity)
        {
            throw new System.NullReferenceException();
        }
    }
}